from datetime import datetime, timezone
from app.extensions import db

class Post(db.Model):
    __tablename__ = 'post'

    post_id = db.Column(db.BigInteger, primary_key=True, autoincrement=True, comment='帖子唯一 ID（主键）')
    user_id = db.Column(db.BigInteger, db.ForeignKey('user.user_id'), nullable=False, comment='发帖用户 ID（外键，关联用户表）')
    content = db.Column(db.Text, nullable=False, comment='帖子文案（用户输入的文字内容，至少 1 个字符）')
    like_count = db.Column(db.Integer, nullable=False, default=0, comment='点赞数（初始 0，用户点赞后递增）')
    post_time = db.Column(db.DateTime, nullable=False, comment='发帖时间（自动记录，无需用户输入）')
    is_delete = db.Column(db.Boolean, nullable=False, default=0, comment='软删除标记（0 = 正常，1 = 删除）')
    
    # === 新增字段：收支信息关联 ===
    record_type = db.Column(db.String(10), nullable=False, comment='收支类型: expense/income')
    category_id = db.Column(db.BigInteger, db.ForeignKey('categories.id'), nullable=False, comment='关联分类ID')
    amount = db.Column(db.Numeric(10, 2), nullable=False, comment='金额')
    record_date = db.Column(db.DateTime, nullable=False, comment='收支记录日期')
    # ============================

    # 关联关系
    images = db.relationship('PostImage', backref='post', lazy=True)
    # 关联发帖用户
    user = db.relationship('User', backref='posts')
    # 新增关联分类
    category = db.relationship('Category', backref='posts')

    def __repr__(self):
        return f'<Post {self.post_id} (user_id: {self.user_id})>'
